package com.googlecode.lightest.core

/**
 * The behavior is TaskNode differs from Node in that children() will never
 * include the original value specified when creating the node in the resulting
 * NodeList. That was a curious decision for the implementation of Node. This
 * original value may be obtained by using the nodeValue() method, instead of
 * value(). It is also (hopefully) thread-safe.
 */
class TaskNode extends Node {
    private nodeValue

    TaskNode(Node parent, Object name) {
        super(parent, name)
    }

    TaskNode(Node parent, Object name, Object value) {
        super(parent, name, value)
        nodeValue = value
    }

    TaskNode(Node parent, Object name, Map attributes) {
        this(parent, name, attributes)
    }

    TaskNode(Node parent, Object name, Map attributes, Object value) {
        super(parent, name, attributes, value)
        nodeValue = value
    }
    
    @Override
    List children() {
        return super.children().findAll { it != nodeValue }
    }
    
    @Override
    void setValue(Object value) {
        super.setValue(value instanceof List
            ? Collections.synchronizedList(value) : value);
    }
    
    /**
     * Returns the single, non-List value specified when creating the Node, if
     * any.
     */
    def nodeValue() {
        return nodeValue
    }
}
